var e = require, t = module, n = exports, o = this && this.__awaiter || function(e, n, i, c) {
    return new (i = i || Promise)(function(a, t) {
        function r(e) {
            try {
                s(c.next(e));
            } catch (e) {
                t(e);
            }
        }
        function l(e) {
            try {
                s(c.throw(e));
            } catch (e) {
                t(e);
            }
        }
        function s(e) {
            var t;
            e.done ? a(e.value) : ((t = e.value) instanceof i ? t : new i(function(e) {
                e(t);
            })).then(r, l);
        }
        s((c = c.apply(e, n || [])).next());
    });
}, i = this && this.__generator || function(a, r) {
    var l, s, n, i = {
        label: 0,
        sent: function() {
            if (1 & n[0]) throw n[1];
            return n[1];
        },
        trys: [],
        ops: []
    }, e = {
        next: t(0),
        throw: t(1),
        return: t(2)
    };
    return "function" == typeof Symbol && (e[Symbol.iterator] = function() {
        return this;
    }), e;
    function t(t) {
        return function(e) {
            return function(t) {
                if (l) throw new TypeError("Generator is already executing.");
                for (;i; ) try {
                    if (l = 1, s && (n = 2 & t[0] ? s.return : t[0] ? s.throw || ((n = s.return) && n.call(s), 
                    0) : s.next) && !(n = n.call(s, t[1])).done) return n;
                    switch (s = 0, (t = n ? [ 2 & t[0], n.value ] : t)[0]) {
                      case 0:
                      case 1:
                        n = t;
                        break;

                      case 4:
                        return i.label++, {
                            value: t[1],
                            done: !1
                        };

                      case 5:
                        i.label++, s = t[1], t = [ 0 ];
                        continue;

                      case 7:
                        t = i.ops.pop(), i.trys.pop();
                        continue;

                      default:
                        if (!(n = 0 < (n = i.trys).length && n[n.length - 1]) && (6 === t[0] || 2 === t[0])) {
                            i = 0;
                            continue;
                        }
                        if (3 === t[0] && (!n || t[1] > n[0] && t[1] < n[3])) {
                            i.label = t[1];
                            break;
                        }
                        if (6 === t[0] && i.label < n[1]) {
                            i.label = n[1], n = t;
                            break;
                        }
                        if (n && i.label < n[2]) {
                            i.label = n[2], i.ops.push(t);
                            break;
                        }
                        n[2] && i.ops.pop(), i.trys.pop();
                        continue;
                    }
                    t = r.call(a, i);
                } catch (e) {
                    t = [ 6, e ], s = 0;
                } finally {
                    l = n = 0;
                }
                if (5 & t[0]) throw t[1];
                return {
                    value: t[0] ? t[1] : void 0,
                    done: !0
                };
            }([ t, e ]);
        };
    }
};

Object.defineProperty(n, "__esModule", {
    value: !0
});

var a = function() {
    function e() {
        this.resMap = new Map(), this.resTaskMap = new Map(), this.imageMap = new Map(), 
        this.imageTaskMap = new Map();
    }
    return e.getInstance = function() {
        return this._instance || (this._instance = new e()), this._instance;
    }, e.prototype.load = function(a) {
        !cc.sys.isBrowser || cc.sys.isMobile ? cc.assetManager.loadRemote(a.url, {
            type: ".png"
        }, function(e, t) {
            return e ? (cc.log("load fail", a.url), void (a.fail && a.fail(e))) : (cc.log("load success", a.url), 
            void (a.success && a.success(t)));
        }) : a.success && a.success(a.url);
    }, e.prototype.loadImage = function(s) {
        return o(this, void 0, void 0, function() {
            var e, l = this;
            return i(this, function() {
                return cc.sys.isBrowser && !cc.sys.isMobile ? ( 
                [ 2, null ]) : this.imageMap.has(s.url) ? (cc.log("loadImage already", s.url), [ 2, this.imageMap.get(s.url) ]) : this.imageTaskMap.has(s.url) ? [ 2, this.imageTaskMap.get(s.url) ] : (e = new Promise(function(t, a) {
                    var r = new Image();
                    r.src = s.url, r.onload = function() {
                       
                        var e = new cc.Texture2D();
                        e.initWithElement(r), e.handleLoadedTexture(), e.addRef(), l.imageMap.set(s.url, e), 
                        r = null, l.imageTaskMap.delete(s.url), t(e);
                    }, r.onerror = function(e) {
                        cc.log("loadImage fail", s.url), r = null, l.imageTaskMap.delete(s.url), a(e);
                    };
                }), this.imageTaskMap.set(s.url, e), [ 2, e ]);
            });
        });
    }, e.prototype.loadRes = function(s) {
        return o(this, void 0, void 0, function() {
            var e, l = this;
            return i(this, function() {
                return this.resMap.has(s.url) ? ( [ 2, this.resMap.get(s.url) ]) : this.resTaskMap.has(s.url) ? [ 2, this.resTaskMap.get(s.url) ] : (e = new Promise(function(a, r) {
                    cc.resources.load(s.url, s.type, function(e, t) {
                        e ? (cc.log("loadRes fail", s.url), l.resTaskMap.delete(s.url), r()) : (
                        l.resMap.has(s.url) || ( t.addRef(), l.resMap.set(s.url, t)), 
                        l.resTaskMap.delete(s.url), a(t));
                    });
                }), this.resTaskMap.set(s.url, e), [ 2, e ]);
            });
        });
    }, e.prototype.releaseRes = function(e) {
        var t, a, r;
        this.resMap.has(e.url) ? ((r = this.resMap.get(e.url)).decRef(), r.refCount < 1 ? (this.resMap.delete(e.url), 
        cc.warn("releaseRes success", e.url), null === (t = e.success) || void 0 === t || t.call(e)) : (cc.warn("releaseRes fail", e.url, r.refCount), 
        null === (t = e.fail) || void 0 === t || t.call(e))) : this.imageMap.has(e.url) ? ((r = this.imageMap.get(e.url)).decRef(), 
        r.refCount < 1 ? (this.imageMap.delete(e.url), null === (r = e.success) || void 0 === r || r.call(e)) : null === (a = e.fail) || void 0 === a || a.call(e)) : (cc.log("releaseRes fail", e.url), 
        null === (a = e.fail) || void 0 === a || a.call(e));
    }, e.prototype.loadSpriteFrame = function(a) {
        a.sprite instanceof cc.Sprite && cc.isValid(a.sprite, !0) && this.loadRes({
            url: a.url,
            type: cc.SpriteFrame
        }).then(function(e) {
            var t;
            a.sprite instanceof cc.Sprite && e instanceof cc.SpriteFrame && cc.isValid(a.sprite, !0) ? (a.sprite.spriteFrame = e, 
            null === (t = a.success) || void 0 === t || t.call(a)) : null === (t = a.fail) || void 0 === t || t.call(a);
        }).catch(function() {
            var e;
            null === (e = a.fail) || void 0 === e || e.call(a);
        });
    }, e.prototype.preload = function(e, t, a) {
        var r, l = this;
        0 < e.length ? (r = e.pop(), this.loadRes({
            url: r,
            type: t
        }).then(function() {
            l.preload(e, t);
        }).catch(function() {
            l.preload(e, t);
        })) : a && a();
    }, e._instance = null, e;
}();

n.default = a;